package com.sgb.kjwl.viewmodel.self.goods;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.lifecycle.MutableLiveData;

import com.sgb.capital.utils.SPreferenceUtil;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.business.StorageManEntity;

import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.self.VipPriceMangeListEntity;
import com.sgb.link.router.PathGoods;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VipPriceManageVieModel extends BaseViewModel implements XRecyclerView.LoadingListener {
    public VipPriceManageVieModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public MutableLiveData<List<VipPriceMangeListEntity>> listdata = new MutableLiveData<>();
    public MutableLiveData<List<StorageManEntity>> listdata_Storage = new MutableLiveData<>();
    public MutableLiveData<List<MenuEntity<MenuEntity<MenuEntity>>>> list_GoodsCategory = new MutableLiveData<>();
    public MutableLiveData<String> dialog_show_text = new MutableLiveData<>();
    private int page = 1;
    public boolean isloadmore, isIsloadmore_forStorage;
    public List<IpPickSelectEntity> choice_type_list = new ArrayList<>();

    public String itemMemberStatus = "";//商品状态
    public String storage = "";//仓储地址
    public String storageId = "";//仓储地址no
    public String cid1 = "";//一级分类id
    public String cid3 = "";//三级分类id
    public String goods_search = "";//顶部搜索内容


    private int page_Storage = 1;
    private int size = 20;
    public String search = "";


    public void initDataForType() {
        choice_type_list.clear();
        choice_type_list.add(new IpPickSelectEntity("", "全部"));
        choice_type_list.add(new IpPickSelectEntity("1", "生效中"));
        choice_type_list.add(new IpPickSelectEntity("0", "已失效"));
    }

    private void getData(boolean isloadmore) {
        this.isloadmore = isloadmore;
        page = isloadmore ? page+1 : 1;
        HashMap<String, Object> body = new HashMap<>();
        body.put("page", page);
        body.put("limit", 20);
        if (!TextUtils.isEmpty(itemMemberStatus)) {
            body.put("itemMemberStatus", itemMemberStatus);
        }
        if (!TextUtils.isEmpty(cid1)) {
            body.put("cid1", cid1);
        }
        if (!TextUtils.isEmpty(cid3)) {
            body.put("cid", cid3);
        }
        if (!TextUtils.isEmpty(storageId)) {
            body.put("storage", storageId);
        }
//        if (!TextUtils.isEmpty(storageId)) {
//            body.put("storageId", storageId);
//        }
        if (!TextUtils.isEmpty(goods_search)) {
            body.put("title", goods_search);
        }

        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getMemberPriceAPI().getMemberPriceGoodsPageList(body).enqueue(new ICallBack<BaseEntity<ListResponse<List<VipPriceMangeListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<VipPriceMangeListEntity>>> data) {
                mLoading.postValue(DISMISS_LODING);
                listdata.postValue(data.getData().getList());
            }

            @Override
            public void onFail() {
                super.onFail();
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    //仓储列表
    public void getStorageList(boolean isLodMore) {
        this.isIsloadmore_forStorage = isLodMore;
        page_Storage = isIsloadmore_forStorage ? page_Storage + 1 : 1;
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsReleasedAPI().getStorageList(page_Storage, size, search).enqueue(new ICallBack<BaseEntity<ProListEntity<List<StorageManEntity>>>>() {

            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<StorageManEntity>>> response) {
                mLoading.postValue(DISMISS_LODING);
                listdata_Storage.postValue(response.getData().getList());
            }

            @Override
            public void onFail() {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }


    //获取分类类别
    public void getGoodsCategoryModel() {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsReleasedAPI().materialGoodsCategory(SPreferenceUtil.getInstance().getString(SPreferenceUtil.PROVINCE_ID, "")).enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                mLoading.postValue(DISMISS_LODING);
                if (data.getData() != null && data.getData().size() > 0) {
                    list_GoodsCategory.postValue(data.getData());
                }
            }
            @Override
            public void onFail() {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    //结束当前会员价
    public void finishItemMemberPrice(long itemId, long itemMemberId) {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getMemberPriceAPI().coloseMemberPrice("item/member/finishItemMemberPrice/" + itemId + "/" + itemMemberId).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                mLoading.postValue(DISMISS_LODING);
                onRefresh();
            }
            @Override
            public void onFail() {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }


    //再次设置权限验证
    public void isCanSetingAgin(long itemId, long itemMemberId) {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getMemberPriceAPI().coloseMemberPrice("item/member/againItemMemberPrice/" + itemId + "/" + itemMemberId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null) {
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    ActivityJump.startActivity(PathGoods.AddMemberGoodsActivity, Pair.create("itemId", itemId + ""),Pair.create("oldItemMemberId",itemMemberId+""));
                } else {
                    dialog_show_text.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
                mToast.postValue(t.getMessage());
            }
        });
    }

    @Override
    public void onRefresh() {
        getData(false);
    }

    @Override
    public void onLoadMore() {
        getData(true);
    }
}
